<template>
    <div class="index clearafter">
        <module-head></module-head>
        <div class="content flex">
            <div class="w220">
                <menu-list></menu-list>
            </div>
            <div class="w980 flex_full">
                <div class="flex flex_between">
                  <p class="flex flex_vcenter pdb20">
                    <i class="titleTag"></i>
                    <span class="fRedBig">角色管理</span>
                  </p>
                    <button class="longBtn" @click="editrole()">新增角色</button>
                    <div id="searchbyname">
                        <div class="col-md-6">
                            <input type="text" class="form-control" v-model="content" placeholder="请输入角色名称或编码">
                        </div>
                        <div class="col-md-3">
                            <button class="default blue" @click="searchbyinput">搜索</button>
                        </div>
                    </div>
                </div>
                <div class="servicepeoplelist bg">
                    <table class="table table-hover table-striped">
                        <thead>
                            <tr>
                                <th>角色编码</th>
                                <th>角色名称</th>
                                <th>描述</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="item in rolelists" track-by="$index">
                                <td>{{item.id}}</td>
                                <td>{{item.name}}</td>
                                <td>{{item.description}}</td>
                                <td><span class="glyphicon glyphicon-pencil" @click="editrole(item)"></span><span class="glyphicon glyphicon-trash" @click="delerole(item)"></span>
                                    <span class="glyphicon glyphicon-lock" @click="MenuRoles(item)"></span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <!-- 新增或者修改角色 -->
        <div class="modal fade" id="editrole" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">{{edittit}}</h4>
                    </div>
                    <form class="form-horizontal cb" role="form" id="selectmodule" v-on:submit.prevent="saverole">
                        <div class="modal-body">
                            <div class="form-group">
                                <label class="col-md-3 control-label"><span class="start">*</span>角色名称:</label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="savedata.name" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label"><span class="start">*</span>角色编码:</label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="savedata.id" :disabled="edittit == '修改角色'" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">描述:</label>
                                <div class="col-md-8">
                                    <textarea class="form-control" rows="3" v-model="savedata.description"></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">产品名称:</label>
                                <div class="col-md-8">
                                    <div class="checkbox" v-for="item in prolists">
                                        <label>
                                            <input type="checkbox" value="{{item.code}}" v-model="savedata.relationList">{{item.name}}
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer savebtn">
                            <button type="submit" class="default ok center">确认</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- 删除角色 -->
        <div class="modal fade" id="delerole" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body cbafter">
                        <p class="text-center">确认删除角色：<span class="red">{{currentname}}？</span></p>
                    </div>
                    <div class="modal-footer">
                        <div class="centerbutton">
                            <button @click="confirmdelerole" class="default cancel center">确认删除</button>
                            <button type="button" class="default greynobg center " data-dismiss="modal" aria-hidden="true">取消</button>
                        </div>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal -->
        </div>
        <!-- 角色授权 -->
        <div class="modal fade" id="MenuRoles" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">角色授权</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal cb" role="form">
                            <div class="form-group">
                                <label class="col-md-2 control-label">产品名称:</label>
                                <div class="col-md-8">
                                    <select class="form-control" v-model="proitem">
                                        <option value="frist">请选择</option>
                                        <option v-for="(index,item) in prolists" value="{{item.code+'/'+item.pCode+'/'+item.name}}">{{item.name}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="treecon">
                                <h3>菜单列表</h3>
                                <ul id="treeDemo" class="ztree"></ul>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer savebtn">
                        <button class="default ok center" @click="savemenurole">确认</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import 'assets/lib/bootstrap-3.3.2/css/bootstrap.min.css'
import 'assets/lib/bootstrap-3.3.2/js/bootstrap.min.js'
import 'assets/reset.css'
import Public from 'assets/public.js'
import moduleHead from 'components/module-head'
import menuList from 'components/menu-list'
import 'assets/lib/ztree/css/zTreeStyle/zTreeStyle.css'
import 'assets/lib/ztree/js/jquery.ztree.core.min.js'
import 'assets/lib/ztree/js/jquery.ztree.excheck.js'
export default {
    data() {
            return {
                rolelists: [],
                content: "",
                edittit: "新增角色",
                savedata: {
                    name: "",
                    id: "",
                    description: "",
                    relationList: [],

                },
                currentid: "",
                currentname: "",
                currentproduct: "",
                setting: {
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {
                            "Y": "p",
                            "N": "s"
                        },

                    },
                    data: {
                        simpleData: {
                            enable: true
                        }
                    },
                    callback: {
                        onCheck: this.zTreeOnCheck
                    },
                },
                zTree: {},
                zNodes: [],
                prolists: [],
                proitem: "",
                currentroleid: "",
            }
        },
        computed: {},
        components: {
            moduleHead, menuList
        },
        ready() {
            var that = this;
            this.getlist();
            //获取产品名称列表 下拉框中的
            $.when(Public.commonajax("base.tenantProductsService", "queryProductTenantByPcode", "[null]"))
                .done(function(res) {
                    if (res.code == 200) {
                        that.prolists = res.body;

                    }

                });

        },
        methods: {
            //搜索
            searchbyinput() {
                    this.getlist()
                },
                // 获取列表数据
                getlist() {
                    var that = this;
                    $.when(Public.commonajax("base.roleService", "queryRoleByNameOrCode", "['" + this.content + "']"))
                        .done(function(res) {
                            if (res.code == 200) {
                                that.rolelists = res.body
                            }
                        })
                },
                // 保存修改或新增弹框
                editrole(item) {
                    var that = this;
                    $("#editrole").modal("show");
                    if (item) {
                        this.edittit = "修改角色";
                        $.when(Public.commonajax("base.roleService", "getById", "['" + item.id + "']"))
                            .done(function(res) {
                                if (res.code == 200) {
                                    var temarr = [];
                                    if (res.body.relationList.length > 0) {
                                        $.each(res.body.relationList, function(index, el) {
                                            temarr.push(el.productCode);
                                        });
                                    }
                                    that.savedata = res.body;
                                    that.savedata.relationList = temarr;

                                }
                            })
                    } else {
                        this.edittit = "新增角色";
                        this.savedata = {
                                name: "",
                                id: "",
                                description: "",
                                relationList: [],

                            }

                    }
                },
                // 保存修改或新增
                saverole() {
                    var that = this;
                    var temarr = [];
                    $.each(this.savedata.relationList, function(index, el) {
                        var temobj = {
                            productCode: el
                        }
                        temarr.push(temobj);
                    });
                    that.savedata.relationList = temarr;
                    if (this.edittit == "修改角色") {
                        $.when(Public.commonajax("base.roleService", "updateRole", "[" + JSON.stringify(that.savedata) + "]"))
                            .done(function(res) {
                                if (res.code == 200) {
                                    Public.ajaxPrompt("修改成功");
                                    $("#editrole").modal("hide");
                                    that.getlist()

                                } else {
                                    Public.ajaxPrompt(res.msg);
                                }
                            })
                    } else {
                        $.when(Public.commonajax("base.roleService", "addRole", "[" + JSON.stringify(that.savedata) + "]"))
                            .done(function(res) {
                                if (res.code == 200) {
                                    Public.ajaxPrompt("新增成功");
                                    $("#editrole").modal("hide");
                                    that.getlist()
                                } else {
                                    Public.ajaxPrompt(res.msg);
                                }
                            })
                    }

                },
                delerole(item) {
                    $("#delerole").modal("show");
                    this.currentid = item.id;
                    this.currentname = item.name;
                },
                confirmdelerole() {
                    var that = this;
                    $.when(Public.commonajax("base.roleService", "deleteRole", "['" + this.currentid + "']"))
                        .done(function(res) {
                            if (res.code == 200) {
                                Public.ajaxPrompt("删除成功");
                                $("#delerole").modal("hide");
                                that.getlist()
                            } else {
                                Public.ajaxPrompt(res.msg);
                            }
                        })
                },
                MenuRoles(item) {
                    var that = this;
                    that.proitem = "frist";
                    $("#MenuRoles").modal("show");
                    this.currentroleid = item.id;
                    // if (that.prolists.length == 0) {
                    //     //获取产品名称列表 下拉框中的
                    //     $.when(Public.commonajax("base.tenantProductsService", "queryProductTenantByPcode", "[null]"))
                    //         .done(function(res) {
                    //             if (res.code == 200) {
                    //                 that.prolists = res.body;
                    //                 // that.proitem = that.prolists[0].code + '/' + that.prolists[0].pCode + '/' + that.prolists[0].name
                    //             }

                    //         });
                    // }


                },
                savemenurole() {
                    var that = this;
                    var checkedarr = this.zTree.getCheckedNodes(true),
                        temarr = [];

                    $.each(checkedarr, function(index, el) {
                        var temobj = {
                            menuId: el.id,
                            actionCode: el.actionCode,
                            roleId: that.currentroleid,
                        };
                        temarr.push(temobj);

                    });
                    $.when(Public.commonajax("base.roleService", "saveMenuRoles", "[" + JSON.stringify(temarr) + "]"))
                        .done(function(res) {
                            if (res.code == 200) {
                                Public.ajaxPrompt("保存成功");
                                $("#MenuRoles").modal("hide");
                            }

                        });
                },
                // 选中或者取消勾选
                zTreeOnCheck(event, treeId, treeNode) {
                    var node = treeNode.getParentNode();
                    if (node && node.children) {
                        if (node.children[0].actionValue) {
                            var filtertedchildren = node.children.filter(function(item) {
                                return item.checked == true;
                            });
                            var actionCode = 0;
                            $.each(filtertedchildren, function(index, el) {
                                actionCode += el.actionValue
                            });
                            node.actionCode = actionCode;
                        }

                    }


                },

        },
        events: {
            // 'data' (data) {
            //     this.modulelists = data;
            // },
        },
        watch: {
            'proitem': function(val, oldval) {
                var that = this;
                // if (val != "frist") {
                var strarr = val.split("/"),
                    code = strarr[0],
                    pcode = strarr[1],
                    selectname = strarr[2],
                    currentroleid = this.currentroleid;
                console.log(this.currentroleid);
                // this.nodeleid = strarr[1];
                var optstr = `['${code}','${pcode}',false,'${currentroleid}']`;
                $.when(Public.commonajax("base.roleService", "queryRoleMenuInfo", optstr))
                    .done(function(res) {
                        if (res.code == 200) {

                            that.zNodes = res.body;

                            $.fn.zTree.init($("#treeDemo"), that.setting, that.zNodes);
                            that.zTree = $.fn.zTree.getZTreeObj("treeDemo");
                            // var nodes =  that.zTree.getCheckedNodes(true);
                            // console.log(nodes);
                        }

                    })
                    // }


            }

        },
}
</script>
<style scoped>
.servicepeoplelist {
    padding: 20px
}

.servicepeoplelist table th {
    text-align: center;
    line-height: 36px;
    height: 36px;
    font-weight: normal;
}

.servicepeoplelist table td {
    line-height: 40px;
    text-align: center;
    color: #898989;
}

.servicepeoplelist table tr td:nth-child(2),
.servicepeoplelist table tr td:nth-child(3) {
    font-weight: bold;
    color: #333;
}

.servicepeoplelist table tr td .glyphicon {
    display: inline-block;
    width: 30px;
    height: 30px;
}

.servicepeoplelist table tr td .glyphicon:hover {
    color: #1dc499;
    cursor: pointer;
}

#searchbyname {
    width: 400px
}

.centerbutton {
    width: 280px;
    margin: 0 auto;
}

.centerbutton button {
    margin-left: 5px;
    display: inline-block;
}

span.red {
    font-weight: bold;
    color: #f86448
}

.treecon {
    padding: 0 20px
}

.treecon h3 {
    font-size: 16px;
    border-left: 3px solid #19ab85;
    padding-left: 15px;
    margin-bottom: 10px
}
</style>
